﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using Galaktika.BI.Silverlight.Controls.PivotGrid.Data;

namespace Galaktika.BI.Silverlight.Controls.PivotGrid.Controls
{
    public class MemberActionEventArgs : EventArgs
    {
        public MemberActionEventArgs(
            MemberInfo member,
            MemberActionType action)
        {
            this.Member = member;
            this.Action = action;
        }

        public readonly MemberInfo Member = null;
        public readonly MemberActionType Action = MemberActionType.Expand;
    }

    public class MemberClickEventArgs : EventArgs
    {
        public MemberClickEventArgs(
            MemberInfo member)
        {
            this.Member = member;
        }

        public MemberClickEventArgs(
            MemberInfo member, Point point) : this(member)
        {
            this.Position = point;
        }

        public readonly MemberInfo Member = null;
        public readonly Point Position = default(Point);
    }

    public abstract partial class MemberControl : UserControl
    {
        public const int CORNER_RADIUS = 3;
        //public const int Default_Width = 100;
        //public const int Default_Height = 25;

        public MemberControl()
        {
            InitializeComponent();
        }

        MemberInfo m_Member = null;
        public MemberInfo Member
        {
            get { return m_Member; }
        }

        public MemberControl(MemberInfo info)
        {
            InitializeComponent();

            m_Member = info;

            Margin = new Thickness(0, 0, 0, 0);

            Border border = LayoutRoot;
            border.BorderBrush = new SolidColorBrush(Colors.DarkGray);
            border.BorderThickness = new Thickness(0, 0, 1, 1);

            border.CornerRadius = new CornerRadius(3);

            GradientStopCollection stops = new GradientStopCollection();
            GradientStop stop0 = new GradientStop();
            stop0.Color = Colors.White;
            GradientStop stop1 = new GradientStop();
            stop1.Color = Colors.LightGray;
            stop1.Offset = 1;
            stops.Add(stop0);
            stops.Add(stop1);
            border.Background = new LinearGradientBrush(stops, 90);

            // На будущее border.Style = (Style)Application.Current.Resources["MyStyle"];

            //Grid root = new Grid();
            border.Child = RootPanel;

            // Плюсик-минусик
            Image image = new Image();
            image.Width = 16;
            image.Height = 16;
            if (Member.DrilledDown)
                image.Source = UriResources.Images.Minus16;
            else
                image.Source = UriResources.Images.Plus16;
            image.MouseLeftButtonUp += new MouseButtonEventHandler(image_MouseLeftButtonUp);

            // Название элемента
            RootPanel.Orientation = Orientation.Horizontal;
            RootPanel.Children.Add(image);
            RootPanel.Children.Add(CaptionText);

            this.SizeChanged += new SizeChangedEventHandler(SpanMemberControl_SizeChanged);
            this.MouseLeftButtonUp += new MouseButtonEventHandler(MemberControl_MouseLeftButtonUp);
        }

        void MemberControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                Raise_ShowContextMenu(new MemberClickEventArgs(this.Member, e.GetPosition(null)));
            }
        }

        void SpanMemberControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            /*if (CaptionText.DesiredSize.Width > CaptionText.ActualWidth)
            {
                if (CaptionText.Text.Length > 0)
                    CaptionText.Text = CaptionText.Text.Substring(0, CaptionText.Text.Length - 1);
                this.UpdateLayout();
            
             * }*/
        }

        public void ShowUpBorder()
        {
            Border border = LayoutRoot;
            border.BorderThickness = new Thickness(border.BorderThickness.Left, 1, border.BorderThickness.Right, border.BorderThickness.Bottom);
        }

        public void ShowLeftBorder()
        {
            Border border = LayoutRoot;
            border.BorderThickness = new Thickness(1, border.BorderThickness.Top, border.BorderThickness.Right, border.BorderThickness.Bottom);
        }

        TextBlock m_CaptionText = null;
        public TextBlock CaptionText
        {
            get {
                if (m_CaptionText == null)
                {
                    m_CaptionText = new TextBlock();
                    m_CaptionText.Margin = new Thickness(2, 0, 0, 0);
                    m_CaptionText.TextAlignment = TextAlignment.Left;
                    m_CaptionText.VerticalAlignment = VerticalAlignment.Center;
                    m_CaptionText.HorizontalAlignment = HorizontalAlignment.Stretch;

                    m_CaptionText.Text = Member.Caption;
                }
                return m_CaptionText; 
            }
        }

        StackPanel m_RootPanel = null;
        public StackPanel RootPanel
        {
            get {
                if (m_RootPanel == null)
                {
                    m_RootPanel = new StackPanel();
                    m_RootPanel.Margin = new Thickness(2, 2, 5, 0);
                    m_RootPanel.VerticalAlignment = VerticalAlignment.Top;
                }
                return m_RootPanel; 
            }
        }



        public RowDefinition Row = null;
        public ColumnDefinition Column = null;

        public event MemberActionEventHandler DrillDownMember;
        private void Raise_DrillDownMember()
        {
            MemberActionEventHandler handler = this.DrillDownMember;
            if (handler != null)
            {
                MemberActionType action = MemberActionType.Expand;
                if(Member.DrilledDown)
                    action = MemberActionType.Collapse;

                handler(this, new MemberActionEventArgs(this.Member, action));
            }
        }

        public event EventHandler<MemberClickEventArgs> ShowContextMenu;
        private void Raise_ShowContextMenu(MemberClickEventArgs args)
        {
            EventHandler<MemberClickEventArgs> handler = this.ShowContextMenu;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        void image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            Raise_DrillDownMember();
        }
    }
}
